package com.bkgy.config;

import com.bkgy.jobs.*;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 9:30 2025/7/10
 */
@Configuration
public class JobsConfig implements ApplicationContextAware {
    @Value("${flatness.job.bxJobCron}")
    private String bxJobCron;
    @Value("${flatness.job.watchOutputJobCron}")
    private String watchOutputJobCron;
    @Value("${flatness.job.profit1870jobCron}")
    private String profit1870jobCron;
    @Value("${flatness.job.profit1870RolljobCron}")
    private String profit1870RolljobCron;
    @Value("${flatness.job.tcmCapacityJobCron}")
    private String tcmCapacityJobCron;
    @Value("${flatness.job.hourTaskJobCron}")
    private String hourTaskJobCron;
    @Value("${flatness.job.outputTaskCron}")
    private String outputTaskCron;
    @Value("${flatness.job.dayDelayJobCron}")
    private String dayDelayJobCron;

    @Value("${processRoller.job.concurrent}")
    private Boolean processRollerJobConcurrent = false;
    @Value("${processRoller.job.triggerCronExpression}")
    private String processRollerJobTriggerCronExpression;

    @Value("${processRoller.job.runFlag}")
    private Boolean processRollerJobRunFlag = false;

    @Value("${rollingSimulation.job.concurrent}")
    private Boolean rollingSimulationJobConcurrent = false;
    @Value("${rollingSimulation.job.triggerCronExpression}")
    private String rollingSimulationJobTriggerCronExpression;

    @Value("${rollingSimulation.job.runFlag}")
    private Boolean rollingSimulationJobRunFlag = false;

    @Value("${roughnessCalculate.job.concurrent}")
    private Boolean roughnessCalculateJobConcurrent = false;
    @Value("${roughnessCalculate.job.triggerCronExpression}")
    private String roughnessCalculateJobTriggerCronExpression;

    @Value("${furnaceRollerTension.job.runFlag}")
    private Boolean furnaceRollerTensionJobRunFlag = false;

    @Value("${furnaceRollerTension.job.concurrent}")
    private Boolean furnaceRollerTensionJobConcurrent = false;
    @Value("${furnaceRollerTension.job.triggerCronExpression}")
    private String furnaceRollerTensionJobTriggerCronExpression;

    @Value("${roughnessCalculate.job.runFlag}")
    private Boolean roughnessCalculateJobRunFlag = false;

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 调度工厂
     *
     * @return
     */
    @Bean
    public SchedulerFactoryBean jobFactory() {
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        List<Trigger> triggerList = new ArrayList<Trigger>();

        Map<String, Trigger> beans = applicationContext.getBeansOfType(Trigger.class);
        for (String s : beans.keySet()) {
            triggerList.add(beans.get(s));
        }
        Trigger[] triggers = new Trigger[triggerList.size()];
        factoryBean.setTriggers(triggerList.toArray(triggers));

        return factoryBean;
    }

    //镀锌生产效益分析--识别新卷进行指标异常识别
    @Bean(name = "profit1870RollJobDetail")
    public MethodInvokingJobDetailFactoryBean profit1870RollJobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(Profit1870RollJob.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean profit1870RollTrigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("profit1870RollJobDetail", JobDetail.class));
        factoryBean.setCronExpression(profit1870RolljobCron);
        return factoryBean;
    }

    //酸轧产量
    @Bean(name = "outputTaskJobDetail")
    public MethodInvokingJobDetailFactoryBean outputTaskJobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(OutputTaskJob.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean outputTaskTrigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("outputTaskJobDetail", JobDetail.class));
        factoryBean.setCronExpression(outputTaskCron);
        return factoryBean;
    }

    @Bean(name = "dayDelayJobDetail")
    public MethodInvokingJobDetailFactoryBean dayDelayJobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(DayDelayJob.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean dayDelayJobTrigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("dayDelayJobDetail", JobDetail.class));
        factoryBean.setCronExpression(dayDelayJobCron);
        return factoryBean;
    }

    //镀锌生产效益分析--计算白班、夜班预计产量
    @Bean(name = "profit1870JobDetail")
    public MethodInvokingJobDetailFactoryBean profit1870JobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(Profit1870Job.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean profit1870Trigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("profit1870JobDetail", JobDetail.class));
        factoryBean.setCronExpression(profit1870jobCron);
        return factoryBean;
    }

    //同步轧卷秒用时数据
    @Bean(name = "bxJobDetail")
    public MethodInvokingJobDetailFactoryBean bxJobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(BxSyncSecJob.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean bxTrigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("bxJobDetail", JobDetail.class));
        factoryBean.setCronExpression(bxJobCron);
        return factoryBean;
    }

    //监听产量 L3记录
    @Bean(name = "watchOutputJobDetail")
    public MethodInvokingJobDetailFactoryBean tcmJobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(WatchOutputJob.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean tcmTrigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("watchOutputJobDetail", JobDetail.class));
        factoryBean.setCronExpression(watchOutputJobCron);
        return factoryBean;
    }

    //计算上一日的产能利用率、报表等
    @Bean(name = "tcmCapacityJobDetail")
    public MethodInvokingJobDetailFactoryBean tcmCapacityJobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(TcmCapacityJob.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean tcmCapacityTrigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("tcmCapacityJobDetail", JobDetail.class));
        factoryBean.setCronExpression(tcmCapacityJobCron);
        return factoryBean;
    }

    @Bean(name = "hourTaskJobDetail")
    public MethodInvokingJobDetailFactoryBean hourTaskJobDetail() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setTargetObject(applicationContext.getBean(HourTaskJob.class));
        detail.setTargetMethod("exec");
        return detail;
    }

    @Bean
    public CronTriggerFactoryBean hourTaskTrigger() {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(applicationContext.getBean("hourTaskJobDetail", JobDetail.class));
        factoryBean.setCronExpression(hourTaskJobCron);
        return factoryBean;
    }

    /**
     * 工艺辊定时任务触发器
     */
    @Bean
    public CronTriggerFactoryBean processRollerJobTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(applicationContext.getBean("processRollerJob", JobDetail.class));
        trigger.setCronExpression(processRollerJobTriggerCronExpression);
        return trigger;
    }

    /**
     * 工艺辊定时任务
     */
    @Bean(name = "processRollerJob")
    public MethodInvokingJobDetailFactoryBean processRollerJob() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setConcurrent(processRollerJobConcurrent);
        detail.setTargetObject(applicationContext.getBean(ProcessRollerJobs.class));
        if (processRollerJobRunFlag) {
            detail.setTargetMethod("run");
        }
        return detail;
    }

    /**
     * 轧制仿真计算定时任务触发器
     */
    @Bean
    public CronTriggerFactoryBean rollingSimulationJobTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(applicationContext.getBean("rollingSimulationJob", JobDetail.class));
        trigger.setCronExpression(rollingSimulationJobTriggerCronExpression);
        return trigger;
    }

    /**
     * 轧制仿真计算定时任务
     */
    @Bean(name = "rollingSimulationJob")
    public MethodInvokingJobDetailFactoryBean rollingSimulationJob() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setConcurrent(rollingSimulationJobConcurrent);
        detail.setTargetObject(applicationContext.getBean(RollingSimulationJobs.class));
        if (rollingSimulationJobRunFlag) {
            detail.setTargetMethod("run");
        }
        return detail;
    }

    /**
     * 计算现粗糙度任务触发器
     */
    @Bean
    public CronTriggerFactoryBean roughnessCalculateJobTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(applicationContext.getBean("roughnessCalculateJob", JobDetail.class));
        trigger.setCronExpression(roughnessCalculateJobTriggerCronExpression);
        return trigger;
    }

    /**
     * 计算现粗糙度定时任务
     */
    @Bean(name = "roughnessCalculateJob")
    public MethodInvokingJobDetailFactoryBean roughnessCalculateJob() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setConcurrent(roughnessCalculateJobConcurrent);
        detail.setTargetObject(applicationContext.getBean(RoughnessCalculateJobs.class));
        if (roughnessCalculateJobRunFlag) {
            detail.setTargetMethod("run");
        }
        return detail;
    }

    /**
     * 同步炉辊张力任务触发器
     */
    @Bean
    public CronTriggerFactoryBean furnaceRollerTensionJobTrigger() {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(applicationContext.getBean("furnaceRollerTensionJob", JobDetail.class));
        trigger.setCronExpression(furnaceRollerTensionJobTriggerCronExpression);
        return trigger;
    }

    /**
     * 同步炉辊张力定时任务
     */
    @Bean(name = "furnaceRollerTensionJob")
    public MethodInvokingJobDetailFactoryBean furnaceRollerTensionJob() {
        MethodInvokingJobDetailFactoryBean detail = new MethodInvokingJobDetailFactoryBean();
        detail.setConcurrent(furnaceRollerTensionJobConcurrent);
        detail.setTargetObject(applicationContext.getBean(FurnaceRollerTensionJobs.class));
        if (furnaceRollerTensionJobRunFlag) {
            detail.setTargetMethod("run");
        }
        return detail;
    }
}
